package com.example.bankmanage.controller;
//import com.example.bankmanage.Common.UserContext;
import com.example.bankmanage.Common.UserContext;
import com.example.bankmanage.Common.UserInfoDTO;
import com.example.bankmanage.dto.rep.UpdateEmpInfoServiceReqDTO;
import com.example.bankmanage.dto.rep.UserLoginReqDTO;
import com.example.bankmanage.dto.rep.UserRegisterReqDTO;
import com.example.bankmanage.dto.resq.UserLoginRespDTO;
import com.example.bankmanage.entity.users;
import com.example.bankmanage.result.Result;
import com.example.bankmanage.service.UserService;
//import com.example.bankmanage.util.ThreadLocalUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Optional;

import static com.example.bankmanage.constant.RedisCacheConstant.USER_LOGIN_KEY;
import static com.example.bankmanage.convention.errorcode.UserErrorCodeEnum.USER_REGISTER_SUCCESS;

/**
 * 执行用户相关的操作
 */
@RestController
@RequestMapping("/user")
@Slf4j
public class UserController {
    @Autowired
    private UserService userService;

    @PostMapping("/register")
    public Result register(@RequestBody UserRegisterReqDTO userRegisterReqDTO) {
        System.out.println("调用了注册");
        userService.register(userRegisterReqDTO);
        return Result.success(USER_REGISTER_SUCCESS);
    }

    @PostMapping(value = "/login")
    public Result login(@RequestBody UserLoginReqDTO userLoginReqDTO) {
        UserLoginRespDTO userLoginRespDTO = userService.login(userLoginReqDTO);
        return Result.success(userLoginRespDTO);
    }
    @GetMapping("/empInfo")
    public Result<users> getEmpInfoService(){
//        System.out.println(stringRedisTemplate.opsForHash().get(USER_LOGIN_KEY + username, token));
//        System.out.println(UserContext.getUserId());
//        System.out.println(Optional.ofNullable(ThreadLocalUtil.get()));
        UserInfoDTO userInfoDTO = UserContext.getUserCopy();
        System.out.println(userInfoDTO);
        log.info("获取用户信息执行了");
        users users1 = userService.getEmpInfoService();
        return Result.success(users1);
    }
    @PutMapping("/update")
    public Result updateEmpInfoService(UpdateEmpInfoServiceReqDTO requestParam){
//        System.out.println(UserContext.getUserId());
        userService.updateEmpInfoService(requestParam);
        return Result.success("用户基本信息修改成功");
    }

}
